package cn.lyvust.demo.shili02;

import cn.lyvust.demo.shili01.NewTitle;

import java.util.LinkedList;
import java.util.List;

/**
 * Description:
 * date: 2021-5-10 21:04
 *
 * @author Guo_guo
 * @since JDK 1.8
 */
public class NewTitleDemo {
    public static void main(String[] args) {
        // 具体实现步骤
        // 1、创建多个各类新闻标题对象
        NewTitle car = new NewTitle(1, "汽车", "管理员");
        NewTitle medical = new NewTitle(2, "医学", "管理员");
        NewTitle item1 = new NewTitle(3, "计算机", "管理员");
        NewTitle item2 = new NewTitle(4, "物联网", "管理员");
        // 2、创建存储各类新闻标题的集合对象
        LinkedList newsTitleList = new LinkedList();
//        List newsTitleList = new LinkedList();
        //泛型
//        List<NewTitle> newsTitleList = new LinkedList();
        // 3、添加头条新闻标题和末尾标题
        newsTitleList.addFirst(car);
        newsTitleList.addLast(medical);
        newsTitleList.add(item1);
        newsTitleList.add(item2);
        // 4、获取头条、以及最末条新闻标题
        NewTitle first = (NewTitle) newsTitleList.getFirst();
        System.out.println("头条的新闻标题为:" + first.getTitleName());
        NewTitle last = (NewTitle) newsTitleList.getLast();
        System.out.println("排在最后的新闻标题为:" + last.getTitleName());
        // 5、删除头条和最末条新闻标题
        NewTitle firstNews=(NewTitle)newsTitleList.removeFirst();
        System.out.println("删除的头条新闻标题为:"+firstNews.getTitleName());
        NewTitle lastNews=(NewTitle)newsTitleList.removeLast();
        System.out.println("删除的末条新闻标题为:"+lastNews.getTitleName());
        System.out.println("删除后剩余的新闻条数："+newsTitleList.size());

        System.out.println("第一种遍历ArrayList：");
        for (int i = 0; i < newsTitleList.size(); i++) {
            NewTitle title = (NewTitle) newsTitleList.get(i);
            System.out.println(i + 1 + ":" + title.getTitleName());
        }
        System.out.println("第二种遍历ArrayList：");
        for (Object obj : newsTitleList) {
            NewTitle newTitle = (NewTitle) obj;
            System.out.println(newTitle.getTitleName());
        }
        System.out.println("第三种遍历ArrayList：");
        newsTitleList.forEach(obj -> {
            NewTitle newTitle = (NewTitle)obj;
            System.out.println(newTitle.getTitleName());
        });
    }
}
